package com.example.jucdemo.countDownLatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author by KoVaVo
 * @Date 2021-09-12 16:54
 * @Desc: 并发的测试countDownLatch
 */
public class Parallellimit {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        CountDownLatch countDownLatch = new CountDownLatch(10);
        for (int i = 0; i<10; i++){
            countRunnable countRunnable = new countRunnable(countDownLatch);
            executorService.execute(countRunnable);
        }
    }
}

class countRunnable implements Runnable {

    private CountDownLatch countDownLatch;

    public countRunnable(CountDownLatch countDownLatch) {
        this.countDownLatch = countDownLatch;
    }

    @Override
    public void run() {
        try {
            synchronized (countDownLatch){
                countDownLatch.countDown();
                System.out.println("线程："+countDownLatch.getCount());
            }
            countDownLatch.await();
            System.out.println("线程执行："+(10-countDownLatch.getCount()));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
